package edu.cs346.group5.ui.forumpages;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JButton;

import edu.cs346.group5.datastores.Item;
import edu.cs346.group5.datastores.Topic;
import edu.cs346.group5.logic.AddViewTopic;
import edu.cs346.group5.ui.MenuFactory;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComboBox;

public class add_topic extends JFrame {

	private JPanel contentPane;
	private JTextField txtTopicName;
	private JTextField txtImageUrl;
	private JTextField txtDescription;
	private JComboBox comboBox;
	private JTextField txtReferenceUrl;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public add_topic() {
		setJMenuBar(MenuFactory.getMenu(this));
		setTitle("Add New Topic");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow]", "[][][][][][]"));
		
		comboBox = new JComboBox();
		List<HashMap<String, Object>> results = AddViewTopic.GetAllCategories();
		for(int i = 0; i < results.size(); i++)
		{
			comboBox.addItem(new Item((int)results.get(i).get("id"), results.get(i).get("category_name").toString()));
		}
		contentPane.add(comboBox, "cell 0 0,growx");
		
		txtTopicName = new JTextField();
		txtTopicName.setText("Topic Name");
		contentPane.add(txtTopicName, "cell 0 1,growx");
		txtTopicName.setColumns(10);
		
		txtReferenceUrl = new JTextField();
		txtReferenceUrl.setText("Reference URL");
		contentPane.add(txtReferenceUrl, "cell 0 2,growx");
		txtReferenceUrl.setColumns(10);
		
		txtImageUrl = new JTextField();
		txtImageUrl.setText("Image URL");
		contentPane.add(txtImageUrl, "cell 0 3,growx");
		txtImageUrl.setColumns(10);
		
		txtDescription = new JTextField();
		txtDescription.setText("Description");
		contentPane.add(txtDescription, "flowx,cell 0 4,growx");
		txtDescription.setColumns(10);
		
		JButton btnNewButton = new JButton("Add Topic");
		btnNewButton.addMouseListener(new MouseAdapter() {
			//Handles adding a new topic
			@Override
			public void mouseClicked(MouseEvent e) {
				Topic topic = new Topic();
				topic.setTopicName(txtTopicName.getText());
				topic.setDescription(txtDescription.getText());
				topic.setImageUrl(txtImageUrl.getText());
				topic.setReferenceURL(txtReferenceUrl.getText());
				topic.setCategoryId(((Item) comboBox.getSelectedItem()).getId());
				AddViewTopic.AddTopic(topic);
				view_topic x = new view_topic(topic);
				x.setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnNewButton, "cell 0 5,alignx right");
	}

}
